package com.apexsoft.monitor.front.console.userlog.service;

import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.JsonResult;
import com.apexsoft.monitor.front.console.userlog.dao.UserLogDao;
import com.apexsoft.monitor.front.console.userlog.om.UserLog;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 
 * @author lsj
 *
 */
@Service("userLogService")
public class UserLogService {
	
	@Autowired
	@Qualifier("userLogDao")
	private UserLogDao userLogDao = null;
	
	public void addMonitorWargingInfo(UserLog log){
		userLogDao.insert(log);
	}
	
	public JSONObject queryUserLogByPage (String key) {
		
		JSONObject result = new JSONObject();
		List<UserLog> list = (List<UserLog>) userLogDao.queryUserLogByPage(key);
		result.put(Result.TOTAL, list.size());
		result.put("root", list);
		return result;
	}

	public UserLog queryDetail (long id) {
		return userLogDao.selectById(id);
	}

	public JSONObject listByPageForEasyui (int page, int pageCount, String remake, String userName, String ip, String startTime, String endTime) {
		try{
			UserLog user = new UserLog();
			Page<UserLog> pageDdApp = new Page<UserLog>(page, pageCount);
			EntityWrapper<UserLog> entityWrapper = new EntityWrapper<UserLog>(user);
			entityWrapper.orderBy("create_time", false);
			if(StringUtils.isNotEmpty(remake)){
				entityWrapper.like("remake",remake);
			}
			if(StringUtils.isNotEmpty(userName)){
				entityWrapper.in("user_id",userName);
			}
			if(StringUtils.isNotEmpty(ip)){
				entityWrapper.like("ip",ip);
			}
			if(StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)){
				entityWrapper.between("create_time",startTime, endTime);
			}
			List<UserLog> ddApps = userLogDao.selectPage(pageDdApp, entityWrapper);
			int count = userLogDao.selectCountByEw(entityWrapper);
			JSONObject obj = new JSONObject();
			obj.put(Result.TOTAL, count);
			obj.put("rows", ddApps);
			return new JsonResult(true,"获取用户日志列表成功！").putObject(obj).getResult();
		}catch(Exception e){
			return new JsonResult().getExceptionResult(e);
		}
	}
}
